package nowcoder.meituan;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class TestCode2 {

    /**

     5
     8 6 3 3 8

     5
     1 6 3 3 8

     */

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }
        int pre = 0;
        List<Integer> q = new LinkedList<>();
        q.add(a[0]);
        long res = 0;
        for(int i = 1; i < n; i++){
            boolean flag = false;
            for(int j = q.size()-1; j >= 0; j--){
                if(q.get(j) < a[i]){
                    pre = q.get(j);
                    res += (i+1)*pre;
                    if(q.size()==j+1){
                        q.add(a[i]);
                    }else{
                        if(q.get(j+1) > a[i]){
                            q.add(j+1, a[i]);
                        }
                    }
                    flag = true;
                    break;
                }
            }
            if(!flag) q.add(0, a[i]);
        }
        System.out.println(res);
    }

}
